#include "p2head.h"

extern Lock waitingRoomLock;
extern Lock wrnLock;
extern Lock nwLock;
extern Lock nwwLock;
extern Condition waitingRoomCondition;
extern Condition wrnWaiting;
extern Condition nurseWaiting;	//nurse wait for waiting room nurse
extern Condition nwCondition;
extern Condition nwwCondition;
extern waitingRoomMoitor WRM;
extern int testcontrol;
extern bool wrncv;

void waitingRoomNurse(){

	int num=0; //for test

	int wncount=1;	//let wrn interacts with both patient and nurse
	struct examSheet * wrnSheet;
	struct form* myform;
	
	while(true){

		nwLock.Acquire();
		if(WRM.nurseWaitingCount>0&&wncount){		//nurse is waiting
			if(WRM.PatientWaitingCount!=0)	//patient is waiting, so I will not talk with you next time
				wncount = 0;	//I will talk with a patient next time
			nwCondition.Signal(&nwLock);
			WRM.nurseWaitingCount--;
			nwwLock.Acquire();
			nwLock.Release();			
			if(WRM.waitingNurseCount==0){				
				WRM.nsheet=NULL;			
			} // end of if
			else{
				//give sheet to the nurse				
				WRM.nsheet=(struct examSheet*)WRM.examSheetQueue->Remove();
				WRM.waitingNurseCount--;
			} //end of else
			nwwCondition.Wait(&nwwLock);
			if(testcontrol==2||testcontrol==7)
				if(WRM.nsheet!=NULL)
					printf("Waiting Room nurse gives examination sheet of patient[%d] to Nurse[%d].\n",WRM.nsheet->name,WRM.nurseid);				
			nwwLock.Release();
		}//end of if
		else{
			wncount =1;	//I will talk with a waiting nurse next time
			nwLock.Release();
			waitingRoomLock.Acquire();
			if(WRM.PatientWaitingCount>0){
				waitingRoomCondition.Signal(&waitingRoomLock);
				WRM.wrnStatus = 0;
				WRM.PatientWaitingCount--;
			}
			else{
				WRM.wrnStatus = 1;	//I am free.
			}

			wrnLock.Acquire();	
			waitingRoomLock.Release();						
			wrnWaiting.Wait(&wrnLock);
			
			if(WRM.wrnTask==1){		//give form
				WRM.form= WRM.newform();
				if(testcontrol==2||testcontrol==7)
					if(WRM.patientType==1)
						printf("Waiting Room nurse gives a form to Adult patient [%d].\n", WRM.id);
					else
						printf("Waiting Room nurse gives a form to the Parent of Child patient [%d].\n", WRM.id);

				wrnWaiting.Signal(&wrnLock);		//inform patient to fill out the form
				//wrnWaiting.Wait(&wrnLock);
			}
			else if(WRM.wrnTask==0){		//receive patient form
				myform= WRM.form;
				//initialize the sheet
				if(testcontrol==2||testcontrol==7)
					if(myform->patientType==1)
						printf("Waiting Room nurse accepts the form from Adult Patient with name [%d] and age [%d].\n", myform->name,myform->age);
					else
						printf("Waiting Room nurse accepts the form from Parent with name [%d] and age [%d].\n", myform->name,myform->age);
		
				wrnSheet = WRM.newExamSheet();
				wrnSheet->shot=0;
				wrnSheet->xray=0;
				wrnSheet->status=1;
				wrnSheet->age = myform->age;
				wrnSheet->name = myform->name;
				wrnSheet->patientType = myform->patientType;

				if(testcontrol==2||testcontrol==7)
					if(wrnSheet->patientType==1)
						printf("Waiting Room nurse creates an examination sheet for Adult patient with name [%d] and age [%d].\n",wrnSheet->name,wrnSheet->age);
					else
						printf("Waiting Room nurse creates an examination sheet for Child patient with name [%d] and age [%d].\n",wrnSheet->name,wrnSheet->age);

				WRM.examSheetQueue->Append((void*)wrnSheet);
				WRM.waitingNurseCount++;
				if(testcontrol==2||testcontrol==7)
					if(wrnSheet->patientType==1)
						printf("Waiting Room nurse tells the Adult Patient [%d] to wait in the waiting room for a nurse.\n",wrnSheet->name);
					else
						printf("Waiting Room nurse tells the Adult Parent [%d] to wait in the waiting room for a nurse.\n",wrnSheet->name);
				wrnWaiting.Signal(&wrnLock);		//inform patient to wait
				
			}

			else if(WRM.wrnTask==2){ //nurse with free wrn
				if(WRM.waitingNurseCount==0){
					//printf("No one is waiting_f\n");
					WRM.nsheet=NULL;
					wrnWaiting.Signal(&wrnLock);
				}
				else{
					//give sheet to the nurse
					WRM.nsheet=(struct examSheet*)WRM.examSheetQueue->Remove();
					WRM.waitingNurseCount--;
					wrnWaiting.Signal(&wrnLock);
					}
				wrnWaiting.Wait(&wrnLock);		//make sure nurse get the sheet.
				if(testcontrol==2||testcontrol==7)
					if(WRM.nsheet!=NULL)
						printf("Waiting Room nurse gives examination sheet of patient[%d] to Nurse[%d].\n",WRM.nsheet->name,WRM.nurseid);
			}

		wrnLock.Release();
		}
	}// end of while

}

